package com.aabte.springcloudstudy.compute;

import com.aabte.springcloudstudy.computeservice.ComputeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping
@RestController
public class ComputeController implements ComputeService {

    @Autowired
    private DiscoveryClient client;

    @Value("${spring.application.name}")
    private String serviceId;

    @Override
    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
        log.info("Start ComputeController#add.");
        ServiceInstance instance = client.getInstances(serviceId).get(0);
        Integer r = a + b;
        log.info("End ComputeController#add. host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return r;
    }

}